package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.UserChildDataContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.UserChildDataPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/23 0023
 * 宝宝信息
 */
public class UserChildDataModel implements UserChildDataContract.Model {
    @Override
    public void addUserChild(UserChildDataPresenter presenter,
                             String tenantId,
                             String token,
                             String fatherId,
                             String avatar,
                             String nickname,
                             String birthday,
                             String sex,
                             String height,
                             String weight,
                             String headCircle) {
        try {
            JSONObject object = new JSONObject();
            object.put("fatherId", fatherId);
            object.put("motherId", fatherId);
            object.put("avatar", avatar);
            object.put("name", nickname);
            object.put("nickname", nickname);
            object.put("birthday", birthday);
            object.put("sex", sex);
            object.put("height", height);
            object.put("weight", weight);
            object.put("headCirc", headCircle);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .addUserChild(tenantId, token,"1", requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }
                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }
                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateUserChild(UserChildDataPresenter presenter, String tenantId, String token, String childId,
                                String fatherId, String avatar, String nickname, String birthday,
                                String sex, String height, String weight, String headCircle) {
        try {
            JSONObject object = new JSONObject();
            object.put("id", childId);
            object.put("fatherId", fatherId);
            object.put("motherId", fatherId);
            object.put("avatar", avatar);
            object.put("name", nickname);
            object.put("nickname", nickname);
            object.put("birthday", birthday);
            object.put("sex", sex);
            object.put("height", height);
            object.put("weight", weight);
            object.put("headCirc", headCircle);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .updateUserChild(tenantId, token,"1", requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }
                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }
                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteUserChild(UserChildDataPresenter presenter, String tenantId, String token, String childId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .deleteUserChild(tenantId, token,"1", childId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<BaseBean>() {
                    @Override
                    public void onNextX(@NonNull BaseBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}